import { Component } from '@angular/core';
import { NzMessageService } from 'ng-zorro-antd';

declare var security: any;

@Component({
  selector: 'app-ui-user-password',
  templateUrl: './user.password.component.html'
})
export class UserPasswordComponent {

  modify: any;

  constructor(private msg: NzMessageService) {
    this.modify = {};
    this.modify.checkPwd = true;
  }
  modifyPassword() {
    this.modify = {};
    this.modify.checkPwd = true;
    this.modify.isEditVisible = true;
  }

  closeModify() {
    this.modify = {};
    this.modify.checkPwd = true;
    this.modify.isEditVisible = false;
  }

  savePassword() {
    this.modify.isLoading = true;
    this.modify.isSaveing = true;
    const ower = this;
    security.SecurityDataService.setPassword(this.modify.op, this.modify.np, (data) => {
      if (data) {
        ower.msg.info('修改成功!');
      } else {
        ower.msg.warning('修改失败，密码填写错误！');
      }
      this.modify = {};
      this.modify.checkPwd = true;
      this.modify.isEditVisible = false;
    }, () => {
      ower.msg.info('修改失败!');
      this.modify = {};
      this.modify.checkPwd = true;
      this.modify.isEditVisible = false;
    });
  }

  checkPwd() {
    if (this.modify.np && this.modify.qnp) {
      if (this.modify.np !== this.modify.qnp) {
        this.modify.checkPwd = false;
      } else {
        this.modify.checkPwd = true;
      }
    } else {
      this.modify.checkPwd = false;
    }
  }
}
